<template>
  <el-container style="height: 100%; border: 1px solid pink" class="ela">
    <el-aside width="250px">
      <el-menu :default-openeds="['1', '3']">
        <el-submenu index="1">
          <template #title><i class="el-icon-goods"></i>商品管理</template>
          <el-menu-item-group>
            <el-menu-item index="1-1">
              <router-link to="/product" class="ro">商品表</router-link>
            </el-menu-item>
            <el-menu-item index="1-2">
              <router-link to="/brand" class="ro">品牌表</router-link>
            </el-menu-item>
            <el-menu-item index="1-3">
              <router-link to="/sales" class="ro">商品销量表</router-link>
            </el-menu-item>
            <el-menu-item index="1-4">
              <router-link to="/producttype" class="ro">商品类型表</router-link>
            </el-menu-item>
          </el-menu-item-group>
        </el-submenu>
      </el-menu>
      <el-menu :default-openeds="['2', '3']">
        <el-submenu index="2">
          <template #title><i class="el-icon-shopping-bag-1"></i>购物管理</template>
          <el-menu-item-group>
            <el-menu-item index="2-1">
              <router-link to="/shopcart" class="ro">购物车表</router-link>
            </el-menu-item>
            <el-menu-item index="2-2">
              <router-link to="/shopcartproduct" class="ro">购物车商品表</router-link>
            </el-menu-item>
            <el-menu-item index="2-3">
              <router-link to="/receiving_address" class="ro">收货地址表</router-link>
            </el-menu-item>
          </el-menu-item-group>
        </el-submenu>
      </el-menu>
      <el-menu :default-openeds="['3', '2']">
        <el-submenu index="3">
          <template #title><i class="el-icon-document-checked"></i>订单管理</template>
          <el-menu-item-group>
            <el-menu-item index="3-1">
              <router-link to="/order" class="ro">订单表</router-link>
            </el-menu-item>
            <el-menu-item index="3-2">
              <router-link to="/orderproduct" class="ro">订单商品表</router-link>
            </el-menu-item>
          </el-menu-item-group>
        </el-submenu>
      </el-menu>
      <el-menu :default-openeds="['4', '3']">
        <el-submenu index="4">
          <template #title><i class="el-icon-setting"></i>系统管理</template>
          <el-menu-item-group>
            <el-menu-item index="4-1">
              <router-link to="/user" class="ro">用户表</router-link>
            </el-menu-item>
            <el-menu-item index="4-2">
              <router-link to="/carousel_figure" class="ro">轮播图表</router-link>
            </el-menu-item>
            <el-menu-item index="4-3">
              <router-link to="/search-history" class="ro">搜索历史表</router-link>
            </el-menu-item>
          </el-menu-item-group>
        </el-submenu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header style="text-align: right; font-size: 12px; background-color: powderblue" >
        <el-dropdown>
          <i class="el-icon-setting" style="margin-right: 15px "></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>查看</el-dropdown-item>
            <el-dropdown-item>新增</el-dropdown-item>
            <el-dropdown-item>删除</el-dropdown-item>
            <el-dropdown-item>退出</el-dropdown-item>
            <el-dropdown-item>
              <el-select v-model="currentTheme" placeholder="请选择主题" @change="changeTheme">
                <el-option label="默认主题" value="default"></el-option>
                <el-option label="暗黑主题" value="dark"></el-option>
              </el-select>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <span class="username">{{ user.user_name }}</span>
      </el-header>
      <el-main style="background-color: paleturquoise" class="elm">
        <el-input v-model="searchQuery" placeholder="请输入表名称（如商品表、品牌表等）" style="margin-bottom: 10px;" @keyup.enter.native="handleSearch"></el-input>
        <router-view :search-query="searchQuery"></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      user: {},
      // 默认主题为'default'
      currentTheme: localStorage.getItem('theme') || 'default',
      searchQuery: '',
      tableRouteMap: {
        '商品表': '/product',
        '品牌表': '/brand',
        '商品销量表': '/sales',
        '商品类型表': '/sale',
        '购物车表': '/shopcart',
        '购物车商品表': '/shopcarta',
        '收货地址表': '/receiving_address',
        '订单表': '/order',
        '订单商品表': '/orderproduct',
        '用户表': '/user',
        '轮播图表': '/carousel_figure',
        '搜索历史表': '/search-history'
      }
    }
  },
  mounted() {
    // 读取用户信息
    var user = localStorage.getItem("users")
    this.user = JSON.parse(user)

    // 根据当前主题应用样式
    this.applyTheme(this.currentTheme)
  },
  methods: {
    changeTheme(theme) {
      // 更新当前主题
      this.currentTheme = theme;

      // 更新本地存储的主题
      localStorage.setItem('theme', theme);

      // 应用新的主题样式
      this.applyTheme(theme);
    },
    applyTheme(theme) {
      const body = document.body;
      if (theme === 'dark') {
        body.classList.add('theme-dark');
      } else {
        body.classList.remove('theme-dark');
      }
    },
    handleSearch() {
      const routePath = this.tableRouteMap[this.searchQuery];
      if (routePath) {
        this.$router.push({ path: routePath });
      } else {
        this.$message.error('未找到对应的表名称');
      }
    }
  }
};
</script>

<style>

.ela{
  background-color: pink;
}
.el-menu-item:hover{
  color: pink;
}
.el-header {
  background-color: pink;
  color: pink;
  line-height: 60px;
}

.el-aside {
  background-color: pink;
  color: pink;
}

.el-menu {
  background-color: pink;
}
.elm{
  background-color: pink;
  color: pink;
}

.theme-dark .el-menu-item:hover, .theme-dark .el-submenu__title:hover {
  background-color: grey;
}

.theme-dark .ela {
  background-color: grey;
}
.theme-dark .elm {
  background-color: grey;
}

.ro{
  color: cornflowerblue;
  text-decoration: none;
}

.ro:hover{
  color: cornflowerblue;
}

.username{
  color: palevioletred;
  font-size: 20px;
}
</style>